#-*- coding:utf8 -*-

import os
import sys
import time

from greenlet import greenlet

def Sleep(wtime):
  print '----------------------- sleep ', wtime
  time.sleep(wtime)

def test1(is_block):
    print 12
    Sleep(1)
    gr2.switch(is_block)
    print 34
    if not is_block:
      gr2.switch(is_block)

def test2(is_block):
    print 56
    if not is_block:
      gr1.switch()
    Sleep(2)
    print 78
    if is_block:
      gr1.switch()

gr1 = greenlet(test1)
gr2 = greenlet(test2)


def main(argv):
  # block用于设计test2是否是block的
  is_block = int(argv[1]) if len(argv) > 1 else 0
  gr1.switch(is_block)

if __name__ == '__main__':
    main(sys.argv)
